$(document).ready(function() {
	
	//graph it!
	$(".go").click(function(){
		//get vars
		var errors = 0;
		var dataNames = [];
		var dataValues1 = [];
		var dataValues2 = [];
		
		//test title
		var title = $(".title").val();
		if(title == "" || title == null) {
			alert("You must enter a title before completing the graph.");
			errors++;
			$(".title").parent().parent().addClass("error");
		} else {
			$(".title").parent().parent().removeClass("error");
		}
		
		//test other labels
		/*if(bar) {
			var xaxis = $(".xaxis").val();
			if(xaxis == "" || xaxis == null) {
				alert("You must enter an X-Axis before completing the graph.");
				errors++;
				$(".xaxis").parent().parent().addClass("error");
			} else {
				$(".xaxis").parent().parent().removeClass("error");
			}
			var yaxis = $(".yaxis").val();
			if(yaxis == "" || yaxis == null) {
				alert("You must enter an Y-Axis before completing the graph.");
				errors++;
				$(".yaxis").parent().parent().addClass("error");
			} else {
				$(".yaxis").parent().parent().removeClass("error");
			}
		}*/
		
		//test data rows
		//also fit data into arrays
		$(".data .dataRow").each(function(){
			var material = $(this).find(".material").val();
			var value1 = $(this).find(".value1").val();
			var value2 = $(this).find(".value2").val();
			//alert("material: " + material + "\nvalue1: " + value1 + "\nvalue2: " + value2);
			//errors++;
			
			var mNull = (material == null || material == "") ? true : false;
			var v1Null = (value1 == null || value1 == "") ? true : false;
			var v2Null = (value2 == null || value2 == "") ? true : false;
			
			if( (mNull && v1Null && !v2Null) || (mNull && !v1Null && v2Null) || (!mNull && v1Null && v2Null) ) {
				alert("Every piece of data must have a name and a value.");
				errors++;
				$(this).addClass("error");
			} else {
				if(!mNull && !v1Null && !v2Null) {
					dataNames.push(material);
					dataValues1.push(value1);
					dataValues2.push(value2);
					$(this).removeClass("error");
				}
			}
			
		});
		
		//render graph
		if(errors == 0) {
			//alert("no errors");
			//get data
			var data1 = new Array();
			var data2 = new Array();
			for (var i=0;i<dataValues1.length;i++) {
				data1[i] = parseInt(dataValues1[i]);
				data2[i] = parseInt(dataValues2[i]);
			}
			
			//get options
			var graphOptions =  { 
				// Tell the plot to stack the bars.
				stackSeries: true,
				captureRightClick: true,
			    seriesDefaults:{
			    	renderer:$.jqplot.BarRenderer,
			    	pointLabels: {show: true},
			    	shadow: false
			    },
			    axes: {
			    	xaxis: {
			    		label: 'Materials',
			    		renderer: $.jqplot.CategoryAxisRenderer,
			    		ticks: dataNames
			    	}
			    },
			    title: {
			        text: title,
			        show: true,
			    },
			    legend: {
			        show: true,
			        location: 'e',
			        placement: 'outside',
			        labels: ['Recycled', 'Not recycled']
			      }
			};
			
			//render
			$("#chart2").html("");
			jQuery.jqplot('chart2', [data1, data2], graphOptions);
			
			//switch displays
			$(".preGraph").addClass("hideHeight");
			$(".postGraph").removeClass("hideHeight");
		}
		
	});
	
	
	
	

			
			
});